package cn.ulc.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/14.
 */
public class CalendarUtil {


    public final static SimpleDateFormat SDF_DEFAULT = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat SDF_DEFAULT_FULL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static SimpleDateFormat SDF_WEEK = new SimpleDateFormat("EEEE");
    public final static SimpleDateFormat SDF_TIME = new SimpleDateFormat("HH:mm");
    public final static SimpleDateFormat SDF_SHORT_FULL = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public final static SimpleDateFormat SDF_MONTH = new SimpleDateFormat("yyyy-MM");

    public final static String WEEKTYPE_XINGQI = "星期";
    public final static String WEEKTYPE_ZHOU = "周";

//    public static Date string2Date(String date,SimpleDateFormat format){
//        if(format==null ){
//            format=SDF_DEFAULT;
//        }
//        try{
//            return format.parse(date);
//        }catch (ParseException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
    public static Date stringToDate(String date,SimpleDateFormat format){
        if(format==null ){
            format=SDF_TIME;
        }
        try{
            return format.parse(date);
        }catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }
    public static String dateToString(Date date,SimpleDateFormat format){
        if(date== null) return null;
        if(format==null ){
            format=SDF_DEFAULT;
        }
        return format.format(date);
    }
//    public static String localDateTime2String(LocalDateTime date, String format){
//        if(date== null) return null;
//        if(StringUtils.isEmpty(format)){
//            format="yyyy-MM-dd HH:mm";
//        }
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
//        return  date.format(formatter);
//    }
//    public static String dateToString(Date date,SimpleDateFormat format){
//        if(date== null) return null;
//        if(format==null ){
//            format=SDF_TIME;
//        }
//        return format.format(date);
//    }

    //昨天的此时
    public static Date getYesterdayNow(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        return calendar.getTime();
    }
    //获取是周几
    public static String getWeekName(int number) {
        String result = "";
        switch (number){
            case 0:
                result = "日";
                break;
            case 1:
                result = "一";
                break;
            case 2:
                result = "二";
                break;
            case 3:
                result = "三";
                break;
            case 4:
                result = "四";
                break;
            case 5:
                result = "五";
                break;
            case 6:
                result = "六";
                break;
        }
        return result;
    }
    //获取是周几
//    public static String getWeekName(Date date,String type) {
//        if (date == null) return null;
//        if (StringUtils.isEmpty(type)) type = WEEKTYPE_XINGQI;
//
//        List<String> weekList = new ArrayList<>();
//        weekList.add(0, "日");
//        weekList.add(1, "一");
//        weekList.add(2, "二");
//        weekList.add(3, "三");
//        weekList.add(4, "四");
//        weekList.add(5, "五");
//        weekList.add(6, "六");
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
//        int week = calendar.get(Calendar.DAY_OF_WEEK);
//        return type + weekList.get(week - 1);
//    }
//
//    public static String getWeekName(String date,String type){
//        Date d = string2Date(date,null);
//        if(d == null) return null;
//        return getWeekName(d,type);
//    }
//
//    public static int getWeekInYear(Date date){
//        if(date == null) return -1;
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        return calendar.get(Calendar.WEEK_OF_YEAR);
//    }
//
//    public static int getWeekInYear(String date){
//        Date d = string2Date(date,null);
//        if(d == null) return -1;
//        return getWeekInYear(d);
//    }

    /**
     * 获取n年后的日期
     */
    public static String getDateAfterYears(Integer n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,n);
        Date resultDate = calendar.getTime();
        return sdf.format(resultDate);
    }

    /**
     * 获取几天后的的此时
     */
    public static Date afterDays(Integer n){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,n);
        return calendar.getTime();
    }
    /**
     * 获取几天后的的此时
     */
    public static Date afterDays(Date date, Integer n){
        if(date == null) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,n);
        return calendar.getTime();
    }

    public static Date beginDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}
